from peewee import *
from datetime import datetime
from .base import BaseModel, Shop, User

class Order(BaseModel):
    id: str = CharField(primary_key=True)
    user_id: int = ForeignKeyField(User, to_field='id', backref='orders', default=0)
    item_fee: int = IntegerField(default=0)
    shop_id: int = ForeignKeyField(Shop, to_field='id', backref='orders', default=0)
    state: int = IntegerField(default=1)
    pay_fee: int = IntegerField(default=0)
    pay_state: int = IntegerField(default=1)
    pay_type: int = IntegerField(default=0)
    expr_type: int = IntegerField(default=0)
    expr_state: int = IntegerField(default=1)
    create_at: int = IntegerField(default=datetime.now().timestamp())  # 创建时间
    update_at: int = IntegerField(default=datetime.now().timestamp())  # 更新时间
    class Meta:
        table_name = 'order'

class OrderItem(BaseModel):
    id: int = AutoField(primary_key=True)
    order_id: str = ForeignKeyField(Order, to_field='id', backref='items')
    name: str = CharField(default='')
    thumb: str = CharField(default='')
    price: int = IntegerField(default=0)
    item_id: int = IntegerField(default=0)
    number: int = IntegerField(default=0)
    is_evaluate: int = IntegerField(default=0)
    class Meta:
        table_name = 'order_item'

class OrderAddress(BaseModel):
    order_id: int = ForeignKeyField(Order, to_field='id', backref='address', primary_key=True)
    consignee: str = CharField(default='')
    mobile: str = CharField(default='')
    dist_id: int = IntegerField(default=0)
    detail: str = CharField(default='')
    class Meta:
        table_name = 'order_address'

class Refund(BaseModel):
    order_item_id: int = ForeignKeyField(OrderItem, to_field='id', backref='refund', primary_key=True)
    out_refund_no: str = CharField(default='')
    fee: int = IntegerField(default=0)
    user_id: int = IntegerField(default=0)
    shop_id: int = IntegerField(default=0)
    pay_state: int = IntegerField(default=1)
    expr_state: int = BooleanField(default=1)
    create_at: int = IntegerField(default=datetime.now().timestamp())  # 创建时间
    update_at: int = IntegerField(default=datetime.now().timestamp())  # 更新时间
    class Meta:
        table_name = 'order_refund'
